package com.srwl.mytx.adapter;


import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.srwl.mytx.Constant;
import com.srwl.mytx.R;
import com.srwl.mytx.activity.AdvertisementDetailActivity;
import com.srwl.mytx.activity.PutAdvertisementActivity;
import com.srwl.mytx.domain.Advertisement;
import com.srwl.mytx.domain.LiveRoom;
import com.srwl.mytx.utils.DateUtils;
import com.srwl.mytx.utils.GlideLoadUtils;
import com.srwl.mytx.utils.Utils;

import java.util.Date;
import java.util.List;

public class AdvertisementRecordAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private static final String TAG = "DonationRecordAdapter";
    private final List<Advertisement> advertisementList;
    private final Context context;
    private boolean bol_noData;
    private boolean bol_requestError;


    public AdvertisementRecordAdapter(Context context, List<Advertisement> ordersList) {
        this.advertisementList = ordersList;
        this.context = context;
    }

//    @Override
//    public int getItemViewType(int position) {
//
//        return advertisementList.get(position).getType();
//
//    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == Constant.ITEM_TYPE_NO_DATA) {
            return new NoDataViewHolder(LayoutInflater.from(context).inflate(R.layout.no__data__layout, parent, false));
        } else if (viewType == Constant.ITEM_TYPE_REQUEST_ERROR) {
            return new RequestErrorViewHolder(LayoutInflater.from(context).inflate(R.layout.request_error_layout, parent, false));
        }
        return new MyViewHolder(LayoutInflater.from(context).inflate(R.layout.layout_offer_record_item, parent, false));
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        int itemViewType = getItemViewType(position);
        if (itemViewType == Constant.ITEM_TYPE_NO_DATA) {
            ((NoDataViewHolder) holder).tv_tips.setText("暂无投放记录");
            return;
        }
        if (itemViewType == Constant.ITEM_TYPE_REQUEST_ERROR) {
            return;
        }
        MyViewHolder normalHolder = (MyViewHolder) holder;
        final Advertisement advertisement = advertisementList.get(position);
        List<Integer> time = advertisement.getTime();
        if (time != null) {
            normalHolder.tv_time.setText(time.toString());
        }
        normalHolder.tv_deposit.setText(Utils.moneyFormat(advertisement.getDeposit()));

        holder.itemView.setOnClickListener(v -> {
            AdvertisementDetailActivity.startActivity(context, advertisement.getId());
        });
        LiveRoom liveRoom = advertisement.getLiveRoom();
        if (liveRoom != null) {
            String iconUrl = Utils.getIconUrl(liveRoom);
            // Glide.with(context).load(iconUrl).diskCacheStrategy(DiskCacheStrategy.ALL).into(normalHolder.iv_avatar);
            GlideLoadUtils.glideLoad(context, iconUrl, normalHolder.iv_avatar);
            normalHolder.tv_name.setText(liveRoom.getTitle());
        }
        normalHolder.tv_amount.setText(Utils.moneyFormat(advertisement.getAmount()));
        Date date = advertisement.getUpdated() == null ? advertisement.getCreated() : advertisement.getUpdated();
        normalHolder.tv_update.setText(DateUtils.getTimestampString(date));
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getItemCount() {

        if (bol_requestError || bol_noData) {
            return 1;
        }
        return advertisementList.size();
    }

    @Override
    public int getItemViewType(int position) {
        if (bol_noData) {
            return Constant.ITEM_TYPE_NO_DATA;
        } else if (bol_requestError) {
            return Constant.ITEM_TYPE_REQUEST_ERROR;
        }
        return Constant.ITEM_TYPE_NORMAL;


    }

    public void showNoDataView() {
        bol_noData = true;
        notifyDataSetChanged();
    }

    public void showDataErroView() {
        bol_requestError = true;
        notifyDataSetChanged();
    }

    public void showNormalView() {
        bol_noData = false;
        bol_requestError = false;
        notifyDataSetChanged();
    }


    class MyViewHolder extends RecyclerView.ViewHolder {

        ImageView iv_avatar;
        TextView tv_name;
        TextView tv_amount;
        TextView tv_time;
        TextView tv_deposit;
        TextView tv_update;


        public MyViewHolder(View itemView) {
            super(itemView);
            iv_avatar = itemView.findViewById(R.id.iv_avatar);
            tv_name = itemView.findViewById(R.id.tv_name);
            tv_amount = itemView.findViewById(R.id.tv_amount);
            tv_update = itemView.findViewById(R.id.tv_update);
            tv_time = itemView.findViewById(R.id.tv_time);
            tv_deposit = itemView.findViewById(R.id.tv_deposit);
        }
    }


}